/* 
	========================================================
	Copyright               Everstream                  2000 
	Any reproduction in part or whole is strictly prohibited
	last modified:   bg 06.08.00    [ brian@everstream.com ]
	========================================================
*/

// expiration times
var al_exp = 50400000;						// 14 hour expiration for autolaunch 
var ck_exp = 90 * 24 * 60 * 60 * 1000		// 90 day expiration for promotional window

// browser identification
var agt = navigator.userAgent.toLowerCase();
var is_nav = (agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1) && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1) && (agt.indexOf('webtv')==-1);
var is_mac = agt.indexOf("mac")!=-1;
var is_ie4up = (agt.indexOf("msie") != -1) && (parseInt(navigator.appVersion) >= 4); 
var is_win = agt.indexOf("win")!=-1;

//==========================================================

// cookie functions
function fixDate(date){
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0) 
    date.setTime(date.getTime() - skew);
}

function setCookie(name,value,expires,path,domain,secure){	
	var curCookie = name + "=" + escape(value) +
	    ((expires) ? "; expires=" + expires.toGMTString() : "") +
	    ((path) ? "; path=" + path : "") +
	    ((domain) ? "; domain=" + domain : "") +
	    ((secure) ? "; secure" : "");
	document.cookie = curCookie;
}

function getCookie(name){
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1){
	  begin = dc.indexOf(prefix);
	  if (begin != 0) return null;
	}else
	  begin += 2;
	var end = document.cookie.indexOf(";", begin);
	if(end == -1)
	  end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
}

function DeleteCookie(name,path,domain){
	if(getCookie(name)){
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

//==========================================================

// fires up tuner if cookie value to auto start is true
function autoStart(){
	var cookDate = getCookie("Expiration");
	var today = new Date();
	if(today > cookDate){
		DeleteCookie("Launched");
		DeleteCookie("Expiration");
		DeleteCookie("Tuned");
		var autoURL = "http://tunerc1.va.everstream.com/" + Alias + "/loader.asp";
		location.href = autoURL;
		var now = new Date();
		fixDate(now);
		now.setTime(now.getTime() + al_exp);
		setCookie("Launched", 1, now,"/");
		setCookie("Tuned", 1, now,"/");
		setCookie("Expiration", now, now, "/");
	}
}

// main function 
function promoWin(){
	// test to see if cookies are enabled
	var now = new Date();
	now.setTime(now.getTime() + (60 * 1000));
	setCookie("enabled", 1, now, "/");
	if(is_win && !is_mac && getCookie("enabled")){
		if((is_nav && document.layers) || (document.all && is_ie4up)){
			if(getCookie("SawPromo") != 1){
				var pmP = window.open("/promo/promoWin.htm?id=" + Alias,"Notice","top="+top+",left="+left+",height=300,width=300");
				var now = new Date();
				fixDate(now);
				now.setTime(now.getTime() + ck_exp);
				setCookie("SawPromo", 1, now, "/");
			}else if(getCookie("AutoStart") == 1){
				autoStart();
			}
		}
	}
}

function configWin(){
	if(is_win && !is_mac){
		if((is_nav && document.layers) || (document.all && is_ie4up)){
			var cnfWin = window.open("/promo/internal/turnTunerOnOff.htm","Tuner_Config","top=0,left=0,height=300,width=300");
		}
	}
}

// extract query string information
function getArgs(url){
	var args = new Object();
	var query = url.substring(1);
	var kv = query.split("=");
	if(kv[1].indexOf("#") != -1){
		var len = kv[1].indexOf('#');
		kv[1] = kv[1].substring(0,len);	
	}
	return unescape(kv[1]);
}

// set cookie to automatically start the tuner on load
function setAutoStart(){
	if(document.all){
		if(document.all.radio1[0].checked){
			var bln = "1";
		}else{
			var bln = "0";
		}
	}else{
		if(document.optionGroup.document.frmConfig.radio1[0].checked){
			var bln = "1";
		}else{
			var bln = "0";
		}
	}
	var now = new Date();
	fixDate(now);
	now.setTime(now.getTime() + ck_exp);
	setCookie("AutoStart", bln, now, "/");
	setCookie("Affiliate", getArgs(location.href), now, "/");
}

function setVariableCookie(t){
	var now = new Date();
	fixDate(now);
	now.setTime(now.getTime() + (t * 24 * 30 * 60 * 1000));
	setCookie("SawPromo", 1, now,"/");
}

function checkClose(){
	if(!getCookie("Tuned")){
		var now = new Date();
		fixDate(now);
		now.setTime(now.getTime() + al_exp);
		setCookie("Tuned", 1, now, "/");	
		setCookie("Launched", 1, now, "/");
		setCookie("Expiration", now, now, "/");
	}else{
		setTimeout("window.close();",1250);
	}
}

function resetConfig(cl){
	if(document.all){
		if(document.all.radio1[0].checked){
			var bln = "1";
		}else{
			var bln = "0";
		}
	}else{
		if(document.optionGroup.document.frmConfig.radio1[0].checked){
			var bln = "1";
		}else{
			var bln = "0";
		}
	}
	var now = new Date();
	fixDate(now);
	if(bln){
		var tme = 14;
	}else{
		var tme = 365;
	}
	now.setTime(now.getTime() + tme * 60 * 60 * 1000);
	setCookie("AutoStart", bln, now, "/");
	if(cl){
		var now = new Date();
		fixDate(now);
		now.setTime(now.getTime() + ck_exp);
		setCookie("SawPromo", 1, now, "/");
		setCookie("AutoStart", bln, now, "/");
		window.close();
	}
}

//==========================================================
